package net.jalbum.filters.components;

import java.io.File;
import java.util.Vector;

import javax.swing.filechooser.FileFilter;

public class FileChooserSettings extends FileFilter {


	private static final long serialVersionUID = 5847323048442797541L;
	//this class works as a fileFilter for the JFileChooser and can be set to accept various file types
	private Vector<String> extensions = new Vector<String>();
	private String description = "*.*";
	
	public boolean accept(File f) {
		//check if element should be shown
		
        if (f.isDirectory()) {
        	//directories should always be shown
            return true;
        }
        if (extensions.size()==0){
        	//show file if the file filter has no predefined settings
        	return true;
        }
        //get file extension of the current file
        String extension = f.getName().substring(f.getName().lastIndexOf(".")+1,f.getName().length());
        
        if (extension != null) {
        	for (int i=0; i<extensions.size(); i++){
        		//check list of predefined extensions
        		if (extension.equals(extensions.get(i))){
        			//until it finds one
        			return true;
        		}
        	}
        }
        //... or until the end of the list is reached
        return false;
    }

    //name of the fileFilter
    public String getDescription() {
        return description;
    }
    
    //set the name of the fileFilter
    public void setDescription(String desc){
    	description = desc;
    }
    
    //add an extension
    public void addExtension(String ext){
    	extensions.add(ext);
    }

	public Vector<String> getExtensions() {
		return extensions;
	}

	public void setExtensions(Vector<String> extensions) {
		this.extensions = extensions;
	}


}
